<?php
declare(strict_types=1);
namespace app\admin\service;

use app\common\model\AdminLogModel;
use app\common\model\AdminModel;
use Firebase\JWT\JWT;

class LoginService
{
    public function login(string $userName,string $password): array
    {
        $ret = AdminModel::getDataByUserName($userName);
        if(!$ret){
            return errorJson('账号不正确');
        }
        if($ret['password'] !== md5Password($password)){
            return errorJson('密码不正确');
        }
        //没有问题 写入登录日志
        AdminLogModel::insetLog($ret['id'],'127.0.0.1',1,'');
        AdminModel::setDataByWhere(['id'=>$ret['id']],['low_login_time'=>time()]);
        //生成token
        $token = createToken($ret['id']);
        return successJson(['token'=>$token]);

    }

}